<template>
  <el-dialog
    :title="!dataForm.no ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="设备ID" prop="equipmentid">
      <el-input v-model="dataForm.equipmentid" placeholder="设备ID"></el-input>
    </el-form-item>
    <el-form-item label="设备名称" prop="equipmentname">
      <el-input v-model="dataForm.equipmentname" placeholder="设备名称"></el-input>
    </el-form-item>
    <el-form-item label="设备类型" prop="type">
      <el-input v-model="dataForm.type" placeholder="设备类型"></el-input>
    </el-form-item>
    <el-form-item label="用途" prop="usedfor">
      <el-input v-model="dataForm.usedfor" placeholder="用途"></el-input>
    </el-form-item>
    <el-form-item label="接口类型" prop="interfacetype">
      <el-input v-model="dataForm.interfacetype" placeholder="接口类型"></el-input>
    </el-form-item>
    <el-form-item label="协议类型" prop="protocol">
      <el-input v-model="dataForm.protocol" placeholder="协议类型"></el-input>
    </el-form-item>
    <el-form-item label="断开/连接" prop="isconnect">
      <el-input v-model="dataForm.isconnect" placeholder="断开/连接"></el-input>
    </el-form-item>
    <el-form-item label="是否故障" prop="isfault">
      <el-input v-model="dataForm.isfault" placeholder="是否故障"></el-input>
    </el-form-item>
    <el-form-item label="是否报警" prop="isalarm">
      <el-input v-model="dataForm.isalarm" placeholder="是否报警"></el-input>
    </el-form-item>
    <el-form-item label="报警信息" prop="alarminfo">
      <el-input v-model="dataForm.alarminfo" placeholder="报警信息"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          no: 0,
          equipmentid: '',
          equipmentname: '',
          type: '',
          usedfor: '',
          interfacetype: '',
          protocol: '',
          isconnect: '',
          isfault: '',
          isalarm: '',
          alarminfo: ''
        },
        dataRule: {
          equipmentid: [
            { required: true, message: '设备ID不能为空', trigger: 'blur' }
          ],
          equipmentname: [
            { required: true, message: '设备名称不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '设备类型不能为空', trigger: 'blur' }
          ],
          usedfor: [
            { required: true, message: '用途不能为空', trigger: 'blur' }
          ],
          interfacetype: [
            { required: true, message: '接口类型不能为空', trigger: 'blur' }
          ],
          protocol: [
            { required: true, message: '协议类型不能为空', trigger: 'blur' }
          ],
          isconnect: [
            { required: true, message: '断开/连接不能为空', trigger: 'blur' }
          ],
          isfault: [
            { required: true, message: '是否故障不能为空', trigger: 'blur' }
          ],
          isalarm: [
            { required: true, message: '是否报警不能为空', trigger: 'blur' }
          ],
          alarminfo: [
            { required: true, message: '报警信息不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.no = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.no) {
            this.$http({
              url: this.$http.adornUrl(`/equipinfo/equipinfotable/info/${this.dataForm.no}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.equipmentid = data.equipinfotable.equipmentid
                this.dataForm.equipmentname = data.equipinfotable.equipmentname
                this.dataForm.type = data.equipinfotable.type
                this.dataForm.usedfor = data.equipinfotable.usedfor
                this.dataForm.interfacetype = data.equipinfotable.interfacetype
                this.dataForm.protocol = data.equipinfotable.protocol
                this.dataForm.isconnect = data.equipinfotable.isconnect
                this.dataForm.isfault = data.equipinfotable.isfault
                this.dataForm.isalarm = data.equipinfotable.isalarm
                this.dataForm.alarminfo = data.equipinfotable.alarminfo
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/equipinfo/equipinfotable/${!this.dataForm.no ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'no': this.dataForm.no || undefined,
                'equipmentid': this.dataForm.equipmentid,
                'equipmentname': this.dataForm.equipmentname,
                'type': this.dataForm.type,
                'usedfor': this.dataForm.usedfor,
                'interfacetype': this.dataForm.interfacetype,
                'protocol': this.dataForm.protocol,
                'isconnect': this.dataForm.isconnect,
                'isfault': this.dataForm.isfault,
                'isalarm': this.dataForm.isalarm,
                'alarminfo': this.dataForm.alarminfo
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
